<?php
class home extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
        $today=strtotime(date("Y-m-d",time()));
    	$yesterday_begin=strtotime(date("Y-m-d",strtotime("-1 day")));
        $yesterday_end=strtotime(date("Y-m-d",strtotime("-1 day")))+(24*60*60);
        $week= strtotime(date('Y-m-d', strtotime('this week')));
        $month = strtotime(date('Y-m'));
        //转入
        $income = new stdClass();
        $income->all = '';
        $income->today = '';
        $income->yesterday = '';
        $income->week = '';
        $income->month = '';

        $income->all = $this->sum(1);
        $income->today = $this->sum(1,$today);
        $income->yesterday = $this->sum(1,$yesterday_begin,$yesterday_end);
        $income->week = $this->sum(1,$week);
        $income->month = $this->sum(1,$month);
        //消费
        $comsume = new stdClass();
        $comsume->all = '';
        $comsume->today = '';
        $comsume->yesterday = '';
        $comsume->week = '';
        $comsume->month = '';

        $comsume->all = $this->sum(0);
        $comsume->today = $this->sum(0,$today);
        $comsume->yesterday = $this->sum(0,$yesterday_begin,$yesterday_end);
        $comsume->week = $this->sum(0,$week);
        $comsume->month = $this->sum(0,$month);

        // 总积分
       	$sql = "SELECT sum(integral) FROM jf_member WHERE status = 1";
		$integral = $this->db->get_var($sql);

        $this->smarty->assign('income',$income);
        $this->smarty->assign('comsume',$comsume);
        $this->smarty->assign('integral',$integral);

		$this->display('home.index.html');
	}
	public function sum($type,$start_time='',$end_time=''){
		$total_integral = 0;
		if($end_time){
			$sql = "SELECT sum(integral) FROM jf_member_integral_history WHERE add_time >= {$start_time} AND add_time <= {$end_time}  AND type = {$type}";
			$total_integral = $this->db->get_var($sql);
		}else{
			if($start_time){
				$sql = "SELECT sum(integral) FROM jf_member_integral_history WHERE add_time >= {$start_time} AND type = {$type}";
				$total_integral = $this->db->get_var($sql);
			}else{
				$sql = "SELECT sum(integral) FROM jf_member_integral_history WHERE type = {$type}";
				$total_integral = $this->db->get_var($sql);
			}
			
		}
		return $total_integral;
	}

	//login
	public function login() {
		if (IS_POST) {
			$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
			$password = isset($_POST['password']) ?  $_POST['password'] : '';
			if ($name =='') error('账户不能为空');
			if ($password =='') error('密码不能为空');
			$sql = "SELECT * FROM jf_admin WHERE name='{$name}'";
			$row = $this->db->get_row($sql);
			if ($row) {
				$crypt_password = crypt_password($password, $row->salt);
				if ($crypt_password == $row->password) {
					$this->admin_id = $row->id;
					$this->admin_name = $row->name;
					$_SESSION['admin'] = $row;
					$sql = "UPDATE jf_admin SET last_time='".time()."', last_ip='".ip()."', logins=logins+1 WHERE id='{$row->id}'";
					$this->db->query($sql);
					$_SESSION['admin']->last_ip = ip();
					$_SESSION['admin']->last_time = date("Y-m-d H:i:s",time());
					header("Location: ?app=home&act=index");
					exit;
				} else {
					echo"<meta charset='UTF-8'><script>alert('密码错误！');window.location.href='?app=home&act=login'</script>";
				}
			} else {
				echo"<meta charset='UTF-8'><script>alert('账号不存在！');window.location.href='?app=home&act=login'</script>";
			}
		} else {
			$this->display();
		}
	}

	public function logout(){
		session_unset();
		if (isset($_SESSION['admin'])) unset($_SESSION['admin']);
		if ($this->admin_id>0) $this->admin_id = 0;		
		header("Location: ?app=home&act=login");
	}
}
